﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace fingerWinForm.DataBase
{
    
    public class Users
    {
        static public String userRole;
        public bool IsLogin(string strName, string strPassword)
        {
            // Procedure checks that the login exists in the XML file

            DataSet dsUsers = new DataSet();
            DataRow[] drRows;
            bool ret = false;

            try
            {

                // Read the XML into a Dataset and filter on name and password for a collection of DataRows.             

                dsUsers.ReadXml(MainWindow.startingPath + "Users.xml");

                drRows = dsUsers.Tables[0].Select("username = '" +
                            strName + "' and password = '" + strPassword + "'");
                
                // Code must be implemented when adding users to the list to insure 
                // that there are no 2 users with the same name 
                // if there is a row in the collection then a record was found
                    
                if (drRows.Length > 0)
                {
                    userRole = (String)drRows[0].ItemArray[2];
                    ret = true;
                }
                else
                {
                    ret = false;
                }

            }
            catch (FileNotFoundException e)
            {
                MessageBox.Show("Users.Xml file not found.", "Unable to Authenticate user.", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Application.Exit();
            }
            return ret;
        }

        public GenericPrincipal GetLogin(string strName, string strPassword)
        {
            // Procedure returns a Generic Principal representing the login account

            DataSet dsUsers = new DataSet();
            DataRow[] drRows = null;

            try
            {

                // Read the XML into a Dataset and filter for a collection of DataRows

                dsUsers.ReadXml(MainWindow.startingPath + "Users.xml");

                drRows = dsUsers.Tables[0].Select("username = '" +
                        strName + "' and password = '" + strPassword + "'");

            }
            catch (FileNotFoundException e)
            {
                MessageBox.Show("Users.Xml file not found.", "Shutting Down...", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Application.Exit();
            }

            // Create the Generic Identity representing the User

            GenericIdentity GenIdentity = new GenericIdentity(strName);

            // Define the role membership an array

            string[] Roles = { Convert.ToString(drRows[0]["Role"]), "" };
            GenericPrincipal GenPrincipal = new GenericPrincipal(GenIdentity, Roles);
            return GenPrincipal;
        }

    }

}
